﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OctoOS.UI;
using OctoOS;

public partial class select : OOSPage
{
    class PathInfo
    {
        [PathInfoMember(0)]
        internal long fCategoryID;

        [PathInfoMember(1, true)]
        internal long sCategoryID;
    }

    PathInfo _pathInfo;
    Resource _resource = null;

    protected void Page_Init(object sender, EventArgs e)
    {
        _pathInfo = EnablePathInfo<PathInfo>();
        _resource = DataSource.ClassifiedResources_Get(
            OOSRequest.Current.ResourceID);
        if (!(OOSRequest.Current.IsAuthenticated || _resource.AllowAnonAds))
        {
            OOSRequest.Current.EnsureSignIn();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Helper.Categories.UpdateCategoryCache();
        PageTitle = "Select subcategory";
        SetBreadcrumbs();
        if (_pathInfo.fCategoryID == 0)
        {
            buildCategoryTree(0);
        }
        else if (_pathInfo.sCategoryID == 0)
        {
            buildCategoryTree(_pathInfo.fCategoryID);
        }
        else
        {
            buildCategoryTree(_pathInfo.sCategoryID);
        }
    }

    private void BuildOnePart(Category current)
    {
        if (current != null)
        {
            TextTreeNode rootNode = new TextTreeNode(current.Name);
            if (!current.ReadOnly) rootNode.HRef = Urls.Classifieds.GetAddNewAd(0, current.ID);
            foreach (var child in Helper.Categories.GetChildCategories(current.ID))
            {
                TextTreeNode childNode = new TextTreeNode(child.Name);
                if (!child.ReadOnly) childNode.HRef = Urls.Classifieds.GetAddNewAd(0, child.ID);
                foreach (var grandChild in Helper.Categories.GetChildCategories(child.ID))
                {
                    TextTreeNode grandChildNode = new TextTreeNode(grandChild.Name);
                    if (!grandChild.ReadOnly) grandChildNode.HRef = Urls.Classifieds.GetAddNewAd(0, grandChild.ID);
                    childNode.Nodes.Add(grandChildNode);
                }
                rootNode.Nodes.Add(childNode);
            }
            categoryTree.Nodes.Add(rootNode);
        }
    }

    private void buildCategoryTree(long categoryID)
    {
        if (categoryID != 0)
        {
            Category current = Helper.Categories.GetCategory(categoryID);
            BuildOnePart(current);
        }
        else
        {
            var allCategories = Helper.Categories.GetAllCategories().Where(f=>f.ParentCategoryID == 0);
            if (allCategories != null)
            {
                foreach (var current in allCategories.ToArray())
                {
                    BuildOnePart(current);
                }
            }
        }
        
        
        
    }

    private void SetBreadcrumbs()
    {
        Category firstCategory = Helper.Categories.GetCategory(_pathInfo.fCategoryID);
        Category secondCategory = null;
        if (_pathInfo.sCategoryID != 0)
        {
            secondCategory = Helper.Categories.GetCategory(_pathInfo.sCategoryID);
        }

        AddBreadcrumb(string.Empty, "/");
        if (firstCategory!= null)
        {
            AddBreadcrumb(firstCategory.Name, Urls.Classifieds.GetCategoryInfo(firstCategory.ID));
        }
        if (secondCategory != null)
        {
            AddBreadcrumb(secondCategory.Name, Urls.Classifieds.GetCategoryInfo(secondCategory.ID));
        }
    }
}